package game.ui.core
{
	import engine.core.*;
	
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	
	import game.ui.controls.*;

	public class RxDialogManager
	{
		private var m_dialogMap:Vector.<Vector.<RxUIElement>>;
		//private var m_resurrectDialog:RxAchievementAlert;
		private var m_autoUsePotion:Boolean;
		public static const NOTIFICATION_GROUP_NONE:int = 0;
		public static const NOTIFICATION_GROUP_ACHIEVEMENT:int = 1;
		public static const NOTIFICATION_GROUP_QUEST:int = 2;
		public static const NOTIFICATION_GROUP_PARTY:int = 3;
		public static const NOTIFICATION_GROUP_TOOLTIPS:int = 4;
		private static const NUM_NOTIFICATION_GROUPS:uint = 5;
		private static var s_numModalDialogs:int = 0;
		private static var s_questDialogSequencer:RxDialogSequencer;
		private static var s_questDialogSequencerCallback:Function;
		private static var s_partyInviteContainer:RxUIElementContainer;
		private static var s_instance:RxDialogManager;
		
		public function RxDialogManager()
		{
			s_instance = this;
			
			this.m_dialogMap = new Vector.<Vector.<RxUIElement>>(NUM_NOTIFICATION_GROUPS, true);
			RxStage.GetStage().addEventListener(Event.RESIZE, this.OnStageResize);
			if (s_partyInviteContainer == null)
			{
				s_partyInviteContainer = new RxUIElementContainer();
				s_partyInviteContainer.SetGap(5);
				s_partyInviteContainer.SetReverseChildOrder(true);
				s_partyInviteContainer.addEventListener(Event.RESIZE, this.OnPartyInviteDialogClosed);
				RxUI.AddElementToLayer(s_partyInviteContainer, RxUILayerConstants.NOTIFICATIONS);
			}
		}
		
		protected function OnStageResize(e:Event) : void
		{
			if (s_numModalDialogs > 0)
			{
				RxUI.ToggleMouseShield(true);
			}
			
			PositionInviteContainer();
		}
		
		protected function PositionInviteContainer() : void
		{
			if (s_partyInviteContainer)
			{
				var stage:Stage = RxStage.stage;
				s_partyInviteContainer.x = stage.stageWidth - s_partyInviteContainer.width;
				s_partyInviteContainer.y = stage.stageHeight - 150 - s_partyInviteContainer.height;
			}
		}
		
		public function ShowDialog(element:RxUIElement, IsAdded:Boolean = false, IsTopLayer:Boolean = false, layerLevel:int = -1) : void
		{
			if (layerLevel == -1)
			{
				layerLevel = IsAdded ? (RxUILayerConstants.MODAL_DIALOGS) : (RxUILayerConstants.NOTIFICATIONS);
			}
			if (IsAdded)
			{
				if (s_numModalDialogs == 0)
				{
					RxUI.ToggleMouseShield(true);
				}
				
				s_numModalDialogs = s_numModalDialogs + 1;
				RxUI.AddElementToLayer(element, IsTopLayer ? (RxUILayerConstants.TOP) : (RxUILayerConstants.MODAL_DIALOGS));
				element.addEventListener(Event.REMOVED_FROM_STAGE, OnModalDialogClosed);
			}
			else
			{
				RxUI.AddElementToLayer(element, layerLevel);
			}
		}
		
		public function OnModalDialogClosed(e:Event) : void
		{
			e.target.removeEventListener(Event.REMOVED_FROM_STAGE, OnModalDialogClosed);
			s_numModalDialogs = s_numModalDialogs - 1;
			
			if (s_numModalDialogs <= 0)
			{
				var layer:Sprite = RxUI.GetUILayer(RxUILayerConstants.MODAL_DIALOGS);
				layer.graphics.clear();
			}
		}
		
		protected function OnPartyInviteDialogClosed(e:Event) : void
		{
			PositionInviteContainer();
		}
		
		public function ShowAlert(param1:String, param2:String, param3:Array = null, param4:int = 0, param5:String = null, param6:Function = null, param7:Boolean = false, param8:Boolean = true, param9:Boolean = false) : RxAlert
		{
			var alert:RxAlert = new RxAlert(param1, param2, param3, param4, param5, param6, param7, param8, param9);
			alert.Show(true, true);
			
			return alert;
		}
		
		public static function get instance() : RxDialogManager
		{
			return s_instance;
		}
		
		public function HideAllDialogs() : void
		{
			var _loc_1:Vector.<RxUIElement> = null;
			var _loc_2:RxDialogBase = null;
			if (s_questDialogSequencer)
			{
				s_questDialogSequencer.ExitDialogSequence(false);
				s_questDialogSequencer = null;
			}
			for each (_loc_1 in this.m_dialogMap)
			{
				
				if (_loc_1 == null)
				{
					continue;
				}
				for each (_loc_2 in _loc_1)
				{
					
					if (_loc_2.stage)
					{
						_loc_2.Hide(false);
					}
				}
				_loc_1.length = 0;
			}
			return;
		}
	}
}